package ipt.pru.Fila1;

import ipt.comparator.OrdenarAlumnoPorApellido1;
import ipt.enumeracion.Rol;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.log4j.Logger;

public class ProbarAlumnoHashMap {

	/**
	 * @param args
	 */

	private final static Logger logf1 = Logger
			.getLogger(ProbarAlumnoHashMap.class);

	public static void main(String[] args) {
		FileReader fr;
		BufferedReader br;
		String linea;
		Alumno al;

		HashMap<String, Alumno> ht = new HashMap<String, Alumno>();
		try {

			fr = new FileReader("data/Alumnos.txt");
			br = new BufferedReader(fr);
			linea = "";

			linea = br.readLine();

			while (linea != null) {

				StringTokenizer t = new StringTokenizer(linea, ",");
				al = new Alumno();
				al.setNombre(t.nextToken());
				al.setApellido1(t.nextToken());
				al.setApellido2(t.nextToken());
				al.setEdad(Integer.parseInt(t.nextToken()));
				al.setEmail(t.nextToken());
				al.setDni(t.nextToken());
				String rol = t.nextToken();
				if (rol.equalsIgnoreCase("TRABAJADOR")) {
					al.setRol(Rol.TRABAJADOR);
				} else if (rol.equalsIgnoreCase("JEFE")) {
					al.setRol(Rol.JEFE);
				} else {
					al.setRol(Rol.CLIENTE);
				}
				ht.put(al.getDni(), al);
				al = null;
				linea = br.readLine();
			}

			logf1.info("f1 Prueba HashMap before sort ");

			List<Alumno> lista = new ArrayList<Alumno>(ht.values());
			logf1.debug("Ordenacion del HashMap por un Comparador de Apellido1");
			Collections.sort(lista, new OrdenarAlumnoPorApellido1());

			logf1.debug("Mostramos el HashMap ordenado por Apellido1");
			logf1.info("f1 Prueba HashMap after sort ");

			Iterator<Alumno> i = lista.iterator();

			while (i.hasNext()) {
				Alumno alu = i.next();
				System.out.println(alu.getApellido1() + ", " + alu.getNombre());

			}

			System.out.println("Fin del fichero");
		} catch (Exception e) {
			// System.out.println("ERROR: "+e.getMessage());
			e.printStackTrace();
		}
	}

}
